/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import java.util.Collections;
import java.util.List;

import com.prefabware.commons.Condition;
import com.prefabware.commons.Message;
import com.prefabware.commons.QualifiedName;
import com.prefabware.jmodel.code.CodeBuffer;
import com.prefabware.jmodel.code.JCodeOptions;

/**
 * the declaration of a field, method or an arguemnt
 * 
 * @author sisele_job
 * 
 */
public class JMemberDeclaration extends JDeclarationBase {
	public JMemberDeclaration(QualifiedName qualifiedName, JType type, JModifiers modifiers,
			JVisibility visibility, List<JType> typeArguments) {
		super(qualifiedName, modifiers, visibility);
		Condition.notNull(type, "..type");
		this.type = type;
		if (typeArguments == null) {
			this.typeArguments = Collections.emptyList();
		} else {
			this.typeArguments = typeArguments;
		}		
	}
	public JMemberDeclaration clone(){
		return (JMemberDeclaration) super.clone();
	  }
	/**
	 * the type of this member 
	 * e.g. String for a field <code>private String name;</code>
	 */
	private JType type;

	public JType getType() {
		return type;
	};

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		JMemberDeclaration other = (JMemberDeclaration) obj;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

	@Override
	public String asCode(JCodeOptions options) {
		CodeBuffer buf = new CodeBuffer(options);
		buf.append(this.getVisibilityAndModifier());
		//member like field and method need the type
		if (this.isIncludeTypeInAsCode()) {
			buf.append(this.getType());
		}
		String typeArgumentsCsv = options.toCsv(this.getTypeArguments());
		buf.setInsertLeadingSpace(false);
		buf.appendIfNotNullOrEmpty(typeArgumentsCsv, "<",typeArgumentsCsv,">");
		//in the java declaration allways use the simple name of the field
		buf.setInsertLeadingSpace(true);
		buf.append(this.getName());
		return buf.toString();
	}

	/**
	 * @return true, to include the type in the declaration as code
	 * field and method need it, constructors not.
	 */
	protected boolean isIncludeTypeInAsCode() {
		return true;
	}

	@Override
	public QualifiedName getQualifiedName() {
		Condition
				.isTrue(this.getDeclaringType() != null,
						Message.format(
								"the declaring type of member %s is null. May be you forgott to add it to a JType ?",
								this));
		return super.getQualifiedName();
	}

}
